<?php

/**
 * 自动生成 SysFuncOperationService
 * 
 * Apps/Common/Service/SysFuncOperationService.class.php
 * Zix @ 2015-06-18
 */

namespace Common\Service;

class SysFuncOperationService {

  private static $instance;

  var $default_operation = array(
    array('sort' => '10' , 'name' => '查看') ,
    array('sort' => '20' , 'name' => '添加') ,
    array('sort' => '30' , 'name' => '编辑') ,
    array('sort' => '40' , 'name' => '删除') ,
  );
  
  var $opt_alias = array(
    '查看' => array('index'  , 'read' , 'get','export','search') ,
    '添加' => array('create' , 'add' , 'upload' ,'post') ,
    '编辑' => array('update' , 'set' ,'save') ,
    '删除' => array('delete' , 'remove' ) ,
  );
  
  public static function instance() {
    if (self::$instance == null) {
      $instance = new SysFuncOperationService();
    }
    return $instance;
  }
  
  function get_default_row() {
    return array(
      'sort' => '',
      'name' => '',
    );
  }

  function get_by_id($id) {
    $SysFuncOperation = M('SysFuncOperation');
    $data = $SysFuncOperation->find($id);
    return $data ? $data : array();
  }
  
  function get_opt_name_by_action_name( $action_name ) {
    $action_name_arr = explode('_', $action_name);
    $alias = $action_name_arr[0];
    
    foreach ( $this->opt_alias as $key => $val ) {
      if (in_array($alias, $val) ) {
        return $key ;
      }
    }
  }
    
  function get_by_func_id( $func_id ) {
    $SysFuncOperation = M('SysFuncOperation');
    $data = $SysFuncOperation->where( 'func_id = %d ' , $func_id )->select();
    return $data ? $data : array();
  }
  
  function get_by_cond($config) {
    $default = array(
      'page' => 1,
      'page_size' => 6,
      'func_id' => 0,
      'status' => '',
      'count' => FALSE,
      'order' => 'ASC',
      'sort' => 'sort',
    );

    $config = extend($config, $default);

    $SysFuncOperation = M('SysFuncOperation');

    $where = array();

    if (!empty($config['status'])) {
      $where['status'] = $config['status'];
    }

    if (!empty($config['func_id'])) {
      $where['func_id'] = $config['func_id'];
    }

    if ($config['count']) {
      return $SysFuncOperation->where($where)->count();
    } else {
      $order = $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $SysFuncOperation
          ->where($where)
          ->limit($limit)->order($order)->select();
      //echo $SysFuncOperation->_sql();
      return $data ? $data : array();
    }
  }
  
  function add_default_opteration_by_func_id( $func_id ) {
    $SysFuncOperation = M('SysFuncOperation');
    foreach ( $this->default_operation as $row ) {
      $row['func_id'] = $func_id ;
      $SysFuncOperation->add($row);
    }
  }
  
  function del_by_func_id($func_id) {
    $SysFuncOperation = M('SysFuncOperation');
    $ret = $SysFuncOperation->where('func_id = %d' , $func_id )->delete();
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

  function create($data) {
    $SysFuncOperation = M('SysFuncOperation');
    $ret = $SysFuncOperation->add($data);
    if ($ret) {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    } else {
      return ajax_arr('添加失败', FALSE);
    }
  }

  function update($id, $data) {
    $SysFuncOperation = M('SysFuncOperation');

    $ret = $SysFuncOperation->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $SysRolePermission = M('SysRolePermission');
    $SysRolePermission->where('opt_id = %d' , $ids)->delete();
    
    $SysFuncOperation = M('SysFuncOperation');
    $ret = $SysFuncOperation->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

}
